#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //Connect all GUI widget
    ui->setupUi(this);
    connect(ui->clearButton,SIGNAL(clicked()), this, SLOT(clearButtonClicked()));
    connect(ui->redSlider, SIGNAL(valueChanged(int)), ui->penWidget, SLOT(setRed(int)));
    connect(ui->greenSlider, SIGNAL(valueChanged(int)), ui->penWidget, SLOT(setGreen(int)));
    connect(ui->blueSlider, SIGNAL(valueChanged(int)), ui->penWidget, SLOT(setBlue(int)));
    connect(ui->widthSlider, SIGNAL(valueChanged(int)), ui->penWidget, SLOT(setWidth(int)));
    connect(ui->penWidget, SIGNAL(changePen(QColor,int)), ui->drawingWidget, SLOT(penChanged(QColor, int)));
    connect(ui->penWidget, SIGNAL(changePen(QColor,int)), this, SLOT(penChanged(QColor, int)));
    connect(ui->colorBox, SIGNAL(activated(int)), ui->penWidget, SLOT(setColorCB(int)));
    connect(ui->widthBox, SIGNAL(activated(int)), ui->penWidget, SLOT(setWidthCB(int)));
    connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));

    //Setup predefined colors
    QStringList colors;
    colors << "black" << "red" << "green" << "blue" << "yellow" << "gray" << "white";
    QAbstractItemModel *model1 = new QStringListModel(colors);
    ui->colorBox->setModel(model1);

    //Setup predefined widths
    QStringList widths;
    widths << "thin" << "medium" << "thick";
    QAbstractItemModel *model2 = new QStringListModel(widths);
    ui->widthBox->setModel(model2);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setLineShare(LineShare *lineshare){
    ui->drawingWidget->setLineShare(lineshare);
}

//Clear drawing widget
void MainWindow::clearButtonClicked()
{
   ui->drawingWidget->clear();
}

//Update GUI widget sliders
void MainWindow::penChanged(QColor color, int width)
{
    ui->redSlider->setSliderPosition(color.red());
    ui->redSlider->repaint();
    ui->redNumber->repaint();
    ui->greenSlider->setSliderPosition(color.green());
    ui->greenSlider->repaint();
    ui->greenNumber->repaint();
    ui->blueSlider->setSliderPosition(color.blue());
    ui->blueSlider->repaint();
    ui->blueNumber->repaint();
    ui->widthSlider->setSliderPosition(width);
    ui->widthSlider->repaint();
    ui->widthNumber->repaint();

}
